package com.paradogs.demo.gate.misc;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: yumi
 * @date: 2023/11/23  14:35
 * @Description: TODO
 */
@Slf4j
@Component
public class PlayerOnlineHolder {

    /**
     * 准备登录的玩家
     */
    private Map<String, WillLoginInfo> willLoginMap = new HashMap<>();


    /**
     * 准备登录
     * @param uid
     * @param token
     */
    public void willLogin(Long uid, String token) {
        willLoginMap.put(token, new WillLoginInfo(uid, token));
    }
    public WillLoginInfo get(String token) {
        return willLoginMap.get(token);
    }
    public void removeToken(String token) {
        willLoginMap.remove(token);
    }

    @Data
    public class WillLoginInfo {
        /**
         * 登录凭证
         */
        private String token;
        /**
         * 玩家 ID
         */
        private Long uid;
        /**
         * 创建时间
         */
        private LocalDateTime createTime;
        /**
         * 超时时间
         */
        private LocalDateTime expireTime;

        public WillLoginInfo(Long uid, String token) {
            this.token = token;
            this.uid = uid;
            this.createTime = LocalDateTime.now();
            this.expireTime = LocalDateTime.now().plusMinutes(1L); // 写死 1 分钟超时
        }
    }


}
